//
//  TimeAndCountCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/7.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "TimeAndCountCell.h"

@interface TimeAndCountCell()

@property (weak, nonatomic) IBOutlet UIView *addMinView;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UILabel *allDaysLab;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *countLab;
@property (weak, nonatomic) IBOutlet UILabel *avaCountLab;

@end

@implementation TimeAndCountCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.addMinView.layer.borderColor = RGBColorOne(188).CGColor;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [self.scrollView addGestureRecognizer:tap];
}
- (void)setMaxReserve:(NSInteger)maxReserve
{
    _maxReserve = maxReserve;
    self.avaCountLab.hidden = NO;
    self.avaCountLab.text = [NSString stringWithFormat:@"剩余%zd个名额",maxReserve];
    if (self.isActivity) {
        self.avaCountLab.hidden = NO;
    }else {
        self.avaCountLab.hidden = YES;
    }
}
- (void)setTimeArr:(NSArray *)timeArr
{
    _timeArr = timeArr;
    NSMutableString *str = [NSMutableString string];
    for (NSInteger i=0; i<timeArr.count; i++) {
        NSDate *date = timeArr[i];
        [str appendString:[self transTime:date]];
        [str appendString:@"  "];
    }
    self.allDaysLab.text = [NSString stringWithFormat:@"(共%zd天)",timeArr.count];
    if (self.isActivity) {
        self.allDaysLab.text = @"";
    }
    self.scrollView.contentSize = CGSizeMake([str sizeWithAttributes:@{NSFontAttributeName:Font(16)}].width, 20);
    self.timeLab.text = str;
}
- (void)tapClick
{
    if (self.timeSelectBlock) {
        self.timeSelectBlock();
    }
}
- (IBAction)timeSelect:(UIButton *)btn
{
    if (self.timeSelectBlock) {
        self.timeSelectBlock();
    }
}

- (IBAction)countClick:(UIButton *)btn
{
    if (btn.tag == 0) {
        if ([self.countLab.text integerValue] == 1) {
            return;
        }else {
            self.countLab.text = [NSString stringWithFormat:@"%zd",[self.countLab.text integerValue]-1];
        }
    }else {
        if (self.isActivity) {
            if ([self.countLab.text integerValue]>=self.maxReserve) {
                return;
            }
        }
        self.countLab.text = [NSString stringWithFormat:@"%zd",[self.countLab.text integerValue]+1];
    }
    if (self.countBlock) {
        self.countBlock([self.countLab.text integerValue]);
    }
}


- (NSString *)transTime:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"MM月dd日";
    return [formatter stringFromDate:date];
}

@end
